// http://mobile.tutsplus.com/tutorials/android/ndk-tutorial/

package com.mamlambo.sample.ndk1;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;


public class AndroidNDK1SampleActivity extends Activity
{
	//private static final String TAG = "MyActivity";

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);
		setContentView(R.layout.user_config);
	}
	
	public double distVincenty(double lat1, double lon1, double lat2, double lon2) 
	{
		//http://www.movable-type.co.uk/scripts/latlong-vincenty.html
			//WGS-84	a = 6 378 137 m (±2 m)	b ≈ 6 356 752.314245 m	f ≈ 1 / 298.257223563
			//GRS-80	a = 6 378 137 m	b ≈ 6 356 752.314140 m	f = 1 / 298.257222101
			//Airy 1830	a = 6 377 563.396 m	b = 6 356 256.910 m	f ≈ 1 / 299.3249646
			//Internat’l 1924	a = 6 378 388 m	b ≈ 6 356 911.946 m	f = 1 / 297
			//Clarke mod.1880	a = 6 378 249.145 m	b ≈ 6 356 514.86955 m	f = 1 / 293.465
			//GRS-67	a = 6 378 160 m	b ≈ 6 356 774.719 m	f = 1 / 298.247167
		
		double a = 6378137, b = 6356752.314245,  f = 1/298.257223563;  // WGS-84 ellipsoid params
		double L = Math.toRadians(lon2-lon1);
		double U1 = Math.atan((1-f) * Math.tan(Math.toRadians(lat1)));
		  
		double U2 = Math.atan((1-f) * Math.tan( Math.toRadians(lat2) ));
		double sinU1 = Math.sin(U1), cosU1 = Math.cos(U1);
		double sinU2 = Math.sin(U2), cosU2 = Math.cos(U2);
		  
		double lambda = L, lambdaP, iterLimit = 100;
		
		
		
		double sinSigma;
		double cosSigma;
		double sigma;
		double cosSqAlpha;
		Double cos2SigmaM;
		do 
		{
			double sinLambda = Math.sin(lambda), cosLambda = Math.cos(lambda);
			sinSigma = Math.sqrt((cosU2*sinLambda) * (cosU2*sinLambda) + (cosU1*sinU2-sinU1*cosU2*cosLambda) * (cosU1*sinU2-sinU1*cosU2*cosLambda));
		    
			if (sinSigma==0)
			{
				return 0;
			}  // co-incident points
		    
			cosSigma = sinU1*sinU2 + cosU1*cosU2*cosLambda;
			sigma = Math.atan2(sinSigma, cosSigma);
			double sinAlpha = cosU1 * cosU2 * sinLambda / sinSigma;
			cosSqAlpha = 1 - sinAlpha*sinAlpha;
			cos2SigmaM = cosSigma - 2*sinU1*sinU2/cosSqAlpha;
			
			if ( cos2SigmaM.isNaN() )
			{
				cos2SigmaM = 0d;
			}  // equatorial line: cosSqAlpha=0 (§6)
			
			double C = f/16*cosSqAlpha*(4+f*(4-3*cosSqAlpha));
			lambdaP = lambda;
			lambda = L + (1-C) * f * sinAlpha * (sigma + C*sinSigma*(cos2SigmaM+C*cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)));
		} while (Math.abs(lambda-lambdaP) > 1e-12 && --iterLimit>0);

		
		
		if (iterLimit==0)
		{
			return Double.NaN;
		}  // formula failed to converge

		double uSq = cosSqAlpha * (a*a - b*b) / (b*b);
		double A = 1 + uSq/16384*(4096+uSq*(-768+uSq*(320-175*uSq)));
		double B = uSq/1024 * (256+uSq*(-128+uSq*(74-47*uSq)));
		double deltaSigma = B*sinSigma*(cos2SigmaM+B/4*(cosSigma*(-1+2*cos2SigmaM*cos2SigmaM) - B/6*cos2SigmaM*(-3+4*sinSigma*sinSigma)*(-3+4*cos2SigmaM*cos2SigmaM)));
		double s = b*A*(sigma-deltaSigma);
		  
		//s = s.toFixed(3); // round to 1mm precision
		return s;
		  
//		// note: to return initial/final bearings in addition to distance, use something like:
//		double fwdAz = Math.atan2(cosU2*sinLambda,  cosU1*sinU2-sinU1*cosU2*cosLambda);
//		double revAz = Math.atan2(cosU1*sinLambda, -sinU1*cosU2+cosU1*sinU2*cosLambda);
//		
//		return { distance: s, initialBearing: fwdAz.toDeg(), finalBearing: revAz.toDeg() };
	}
	
	

	public void goToNavigator(View view) 
	{
		//showNavigation("TestFer");
		showNavigation("TestFer", 37.423156,-122.084917);
	}
	
	public void showForm(View view) 
	{
		setContentView(R.layout.main);
	}

	public void showNavigation(String name, double lat, double lon)
	{
//		Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
//				Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
		
//		Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
//				Uri.parse("google.navigation:q=an+address+city");		
		
		
//		Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
//				Uri.parse("geo:0,0?q=37.423156,-122.084917 (" + name + ")")
//			);
		
		
		Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
				Uri.parse("geo:0,0?q=" + lat + "," + lon + " (" + name + ")")
			);
		
		
		startActivity(intent);
	}	
	
}

//---------------------------------------------------------------------------------------------------------

//package com.mamlambo.sample.ndk1;
//
//import android.app.Activity;
//import android.os.Bundle;
//import android.util.Log;
//import android.view.View;
//import android.view.View.OnClickListener;
//import android.widget.Button;
//
//
////import android.widget.RelativeLayout;
//
//public class AndroidNDK1SampleActivity extends Activity
//{
//	// Native related code.
//	static
//	{
//		System.loadLibrary("ndk1");
//	}
//
//	private native void helloLog(String logThis);
//	private native String getString(int value1, int value2);
//
//	private static final String TAG = "MyActivity";
//
//	@Override
//	public void onCreate(Bundle savedInstanceState)
//	{
//		super.onCreate(savedInstanceState);
//
//		final Button button = new Button(this);
//		button.setText("Click me!");
//
//		button.setOnClickListener(new OnClickListener()
//		{
//			public void onClick(View v)
//			{
//				// button.setText("Thanks!");
//				helloLog("This will log to LogCat via the native call!!!!!!!");
//				Log.v(TAG, "Java Managed Log test!!!");
//				
////				String result = getString(5, 2);
////				Log.v(TAG, "Result: " + result);
////				result = getString(105, 1232);
////				Log.v(TAG, "Result2: " + result);
//			}
//		});
//
//		// setContentView(R.layout.main);
//		setContentView(button);
//	}
//}
